$(function () {
    //调用getUserInfo 获取用户信息
    getUserInfo();
    //点击退出按钮事件
    $("#btnLogout").on("click", function () {
        layui.layer.confirm('确定退出登录？', { icon: 3, title: '提示' }, function (index) {
            localStorage.removeItem("token");
            location.href = "login.html";
            layer.close(index);
        });
    });
});

//获取用户相关信息
function getUserInfo() {
    $.ajax({
        method: "GET",
        url: "/my/userinfo",
        success: function (res) {
            if (res.status !== 0) {
                return layui.layer.msg("获取用户信息失败！");
            }
            //调用renderAvatar 渲染用户信息
            renderAvatar(res.data);
        }
    });
}

//渲染用户信息
function renderAvatar(user) {
    let name = user.nickname || user.username;
    $("#welcome").html("欢迎&nbsp;&nbsp;" + name);
    if (user.user_pic !== null) {
        $(".layui-nav-img").attr("src", user.user_pic).show();
        $(".text-avarat").hide();
    } else {
        $(".layui-nav-img").hide();
        let first = name[0].toUpperCase();
        $(".text-avarat").html(first);
    }
}